package com.paomi.client.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.paomi.client.R;


public class MyPopupMenu extends PopupWindow implements View.OnClickListener {
    private Activity activity;
    private View popView;
    private LinearLayout hot, time;
    private TextView tv_hot, tv_time;
    private OnClassifyClickListener clickListener;
    Typeface iconfont;

    public MyPopupMenu(Activity activity) {
        super(activity);
        iconfont = Typeface.createFromAsset(activity.getAssets(), "iconfont.ttf");
        this.activity = activity;
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popView = inflater.inflate(R.layout.classify_pop, null);// 加载菜单布局文件
        this.setContentView(popView);// 把布局文件添加到popupwindow中
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);// 设置菜单的宽度（需要和菜单于右边距的距离搭配，可以自己调到合适的位置）
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);// 获取焦点
        this.setTouchable(true); // 设置PopupWindow可触摸
        this.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);
        darkenBackground(0.6f);

        hot = popView.findViewById(R.id.ll_hot);
        tv_hot = popView.findViewById(R.id.tv_hot);
        time = popView.findViewById(R.id.ll_time);
        tv_time = popView.findViewById(R.id.tv_time);
        tv_time.setTypeface(iconfont);
        tv_hot.setTypeface(iconfont);
        hot.setOnClickListener(this);
        time.setOnClickListener(this);
        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                darkenBackground(1f);
            }
        });
    }

    /**
     * 改变背景颜色
     */
    private void darkenBackground(Float bgcolor) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgcolor;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);

    }

    /**
     * 设置显示的位置
     *
     * @param resourId 这里的x,y值自己调整可以
     */
    public void showLocation(int resourId) {
        showAsDropDown(activity.findViewById(resourId), dip2px(activity, 10),
                dip2px(activity, 5));
    }

    // dip转换为px
    public int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_hot:
                if (clickListener != null) {
                    clickListener.onHotClick();
                }
                break;
            case R.id.ll_time:
                if (clickListener != null) {
                    clickListener.onTimeClick();
                }
                break;
        }
    }

    // 点击监听接口
    public interface OnClassifyClickListener {
        void onHotClick();

        void onTimeClick();
    }

    // 设置监听
    public void setOnClassifyClickListener(OnClassifyClickListener onItemClickListener) {
        this.clickListener = onItemClickListener;
    }
}
